/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package geowiki.utils;

import processing.core.PApplet;
import processing.core.PVector;

/**
 * 
 * @author marctiedemann
 */
public class SkaleInterpolator extends PApplet {

	protected int r0;
	protected int r1;
	protected float s1;
	protected float zoom;

	public SkaleInterpolator(float zoom, int r0, int r1, float s1) {
		this.r0 = r0;
		this.r1 = r1;
		this.s1 = s1;
		this.zoom = zoom;
	}

	public float interpolRadius(float r) {

		// /Scale Interpol algorythmn von "Karten variabler Skalierung"

		float s0 = 1;

		r = r * zoom;

		if (r <= r0) {
			r = r;
		}
		if (r > r0 && r <= r1) {
			r = r + ((sq(r - r0) * (s1 - 1)) / (2 * (r1 - r0)));
		}
		if (r > r1) {
			r = r0 + (r1 - r0) * ((1 + s1) / 2) + (r - r1) * s1;
		}
		return r;
	}

	public PVector interpolVector(PVector center, PVector orgCoord) {

		float R = PVector.dist(center, orgCoord);
		float newR = interpolRadius(R);

		PVector orgFromCenter = new PVector(0, 0, 0);
		PVector.sub(orgCoord, center, orgFromCenter);
		float n = map(newR, 0, R, 0, 1);
		orgFromCenter.mult(n);
		PVector newCoord = PVector.add(orgFromCenter, center);

		return newCoord;
	}

}
